package com.hw.springcloud;

import com.hw.springcloud.entities.CommonResult;
import com.hw.springcloud.entities.Payment;
import com.hw.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Classname: PaymentController
 * @Description: 描述
 * @Date: 2021/6/9
 * @Author: huwei
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    /**
     * 原本是@AllArgsConstructor注解,但是和@Value注解一起用会报错
     * 或者使用@Resource注入,参考8001服务写法(或者使用@Autowired注入)
     */
    @Resource
    private PaymentService paymentService;

    /**
     * 服务发现功能
     */
    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment) {
//        int i = paymentService.create(payment);
//        if ( i == 1) {
//            return new CommonResult(200, "添加成功,port:"+serverPort);
//        } else {
//            return new CommonResult(500, "添加失败,port:"+serverPort);
//        }
        boolean save = paymentService.save(payment);
        if (save) {
            return new CommonResult(200, "添加成功,port:" + serverPort);
        } else {
            return new CommonResult(500, "添加失败,port:" + serverPort);
        }
    }

    @GetMapping("/getById/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id) {
//        return new CommonResult<>(400, "查询成功,port:" + serverPort, paymentService.getPaymentById(id));
        return new CommonResult<>(400, "查询成功,port:" + serverPort, paymentService.getById(id));
    }

    @GetMapping("/getDiscovery")
    public Object getDiscovery() {
        // 获得服务集合
        List<String> services = discoveryClient.getServices();
        for (String server : services) {
            log.info("Server:" + server);
        }
        // 根据服务名称获得服务下实例集合
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            // 获得实例对象
            log.info("Instance:" + instance);
            // 获得实例的服务Id
            log.info("ServiceId:" + instance.getServiceId());
            // 获得主机
            log.info("Host:" + instance.getHost());
            // 获得端口号
            log.info("Port:" + instance.getPort());
            // 获得Uri
            log.info("Uri:" + instance.getUri());
            // 获得实例Id
            log.info("instanceId:" + instance.getInstanceId());
        }
        return this.discoveryClient;
    }


    /**
     * 手写轮询算法测试
     */
    @GetMapping("/lb")
    private String getPaymentLb(){
        return serverPort;
    }

    /**
     * 测试如果等待时间超时,出现的情况
     */
    @GetMapping("/timeout")
    public String paymentOpenFeignTimeOut(){
        log.info("调用超时接口");
        try {
            // 设置线程暂停3秒
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/zipkin")
    public String Zipkin(){
        return "这是Zipkin方法的返回信息";
    }
}
